package com.lagou.rpc.zookeeper.handler;

import com.lagou.rpc.zookeeper.config.ConfigKeeper;
import com.lagou.rpc.zookeeper.handler.impl.ZookeeperRegistryCenter;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;

/**
 * 注册中心工厂类
 */
public class RpcRegistryCenterFactory implements FactoryBean<RpcRegistryCenter>, DisposableBean {

    private RpcRegistryCenter rpcRegistryCenter;

    @Override
    public void destroy() throws Exception {
        if (rpcRegistryCenter!=null){
            rpcRegistryCenter.destroy();
        }
    }

    @Override
    public RpcRegistryCenter getObject() throws Exception {
        if (rpcRegistryCenter == null){
            rpcRegistryCenter = new ZookeeperRegistryCenter(ConfigKeeper.getInstance().getConnectString());
        }
        return rpcRegistryCenter;
    }

    @Override
    public Class<?> getObjectType() {
        return RpcRegistryCenter.class;
    }
}
